﻿using FI.Architecture.Server;
using Sample.BusinessEntities;
using Sample.Common.Transaction;
using Sample.DataAccess;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sample.BusinessComponent
{
    public class PersonBC : BusinessComponentBase
    {
        public PersonModel GetPerson(int id)
        {
            using (var da = ServiceLocator.GetService<PersonDA>())
            {
                return da.GetPerson(id);
            }
        }

        public IEnumerable<PersonModel> GetPersons()
        {
            using (var da = ServiceLocator.GetService<PersonDA>())
            {
                return da.GetPersons();
            }
        }

        public IEnumerable<PersonModel> GetPersonsByCriteria(string name, DateTime? birthdate, out int totalCount, int page = 1)
        {
            using (var da = ServiceLocator.GetService<PersonDA>())
            {
                return da.GetPersonsByCriteria(name, birthdate, page, out totalCount);
            }
        }



        public void AddPerson(PersonModel personModel)
        {
            using (var scope = new TransactionScopeWrapper(System.Transactions.TransactionScopeOption.Required))
            {
                using (var da = ServiceLocator.GetService<PersonDA>())
                {
                    da.AddPerson(personModel);

                }
                using (var da = ServiceLocator.GetService<PersonDA>())
                {
                    da.AddPerson(personModel);

                }
                using (var da = ServiceLocator.GetService<PersonDA>())
                {
                    da.AddPerson(personModel);

                }
                scope.Complete();
            }
        }

        public void UpdatePerson(PersonModel personModel)
        {
            using (var scope = new TransactionScopeWrapper(System.Transactions.TransactionScopeOption.Required))
            {
                using (var da = ServiceLocator.GetService<PersonDA>())
                {
                    da.UpdateNameOfPerson(personModel);

                }
                scope.Complete();
            }
        }
    }
}
